/* 
 * File:   Question.cpp
 * Author: eduardo
 *
 * Created on 6 de diciembre de 2012, 17:53
 */

#include <vector>

#include "Question.h"
#include "AgentDB.h"

Question::Question() {
    _j = _totalDocs = 0;
}

Question::~Question() {
    _terms.clear();
}

void Question::makeQuestion(const string& q) {
    _question = q;
    takeTerms();

    // Recuperamos documentos que contienen los terminos
    for (int i = 0; i < _j; i++) {
        // Recuperamos si el termino existe
        if (AgentDB::getAgentePtr()->existTerm(_terms.at(i))) {
            AgentDB::getAgentePtr()->getIdDocs(_terms.at(i), &_documents);
        }
    }
    // Recuperamos el total de docs
    AgentDB::getAgentePtr()->getTotalDocs(&_totalDocs);

    // Los recorremos asignando la semejanza
    int weights[_terms.size()][_documents.size()];
    map<int, int> mO;
    for (int i = 0; i < _terms.size(); i++) {
        mO = AgentDB::getAgentePtr()->fillOccu(_terms.at(i));
        set<int>::iterator itS = _documents.begin();
        for (int j = 0; j < _documents.size(); j++, itS++) {
            if (mO.find(*itS) != mO.end()) {
                weights[i][j] = mO[*itS];
            } else {
                weights[i][j] = 0;
            }
        }
    }
    
    for (int d = 0; d < _documents.size(); d++) {
        
    }

    // Los mostramos en orden de semejanza


}

void Question::takeTerms() {
    char* str;
    char* question = new char[_question.size() + 1];
    strcpy(question, _question.c_str());
    str = strtok(question, " ,.");
    while (str != NULL) {
        _terms.push_back(str);
        _j++;
        str = strtok(NULL, " ,.");
    }
    delete[] question;
}